﻿using System.Collections;
using System.Runtime.InteropServices;
using System.Text;
using Newtonsoft.Json;

namespace Test
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Title2Crt("E:\\抖音推文\\小说音频及字幕\\字幕\\如果你是胆小鬼第一章少女音1.3倍速.title", "E:\\抖音推文\\小说音频及字幕\\字幕\\如果你是胆小鬼第一章少女音1.3倍速.srt");


            //long startHours = (milliseconds) / (1000 * 60 * 60);
            //long startminutes = (milliseconds / (1000 * 60)) % 60;
            //long startseconds = (milliseconds / 1000) % 60;
            //double startmilliseconds = milliseconds % 1000;

            //Title2Crt();
            //Console.WriteLine("Hello, World!");
            //_ = fortestAsync();
            //Console.WriteLine("xia");
            //Console.ReadLine();
        }

        public static async Task fortestAsync()
        {
            await foreach (int number in GetNumbersAsync())
            {
                Console.WriteLine(number);
            }
        }
        static async IAsyncEnumerable<int> GetNumbersAsync()
        {
            for (int i = 0; i < 5; i++)
            {
                await Task.Delay(1000); // 模拟异步操作
                yield return i;
            }
        }

        public static void Title2Crt(string titlePath,string srtPath)
        {
            using (FileStream fsTitle = new FileStream(titlePath,FileMode.Open,FileAccess.Read))
            {
                using (FileStream fsSrt = new FileStream(srtPath, FileMode.Create, FileAccess.ReadWrite))
                {
                    byte[] buffer = new byte[fsTitle.Length];
                    fsTitle.Read(buffer, 0, buffer.Length);
                    string titleContent = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
                    List<JsonData> jsonDatas = JsonConvert.DeserializeObject<List<JsonData>>(titleContent);
                    TimeSpan startTime = new TimeSpan(0, 0, 0);
                    for (int i = 0,j=1; i < jsonDatas.Count; i++,j++)
                    {
                        StringBuilder strSub = new StringBuilder();
                        //序号
                        strSub.AppendLine(j.ToString());

                        //时间段
                        long startMilliseconds = Convert.ToInt64(Math.Floor(jsonDatas[i].time_begin));
                        long startHour = (startMilliseconds) / (1000 * 60 * 60);
                        long startMinute = (startMilliseconds / (1000 * 60)) % 60;
                        long startSecond = (startMilliseconds / 1000) % 60;
                        long startMillisecond = startMilliseconds % 1000;

                        long endMilliseconds = Convert.ToInt64(Math.Floor(jsonDatas[i].time_end));
                        long endHour = (endMilliseconds) / (1000 * 60 * 60);
                        long endMinute = (endMilliseconds / (1000 * 60)) % 60;
                        long endSecond = (endMilliseconds / 1000) % 60;
                        long endMillisecond = endMilliseconds % 1000;
                        strSub.AppendLine(
                            string.Format("{0}:{1}:{2}.{3}", startHour, startMinute, startSecond, startMillisecond) +
                            " --> "+
                            string.Format("{0}:{1}:{2}.{3}", endHour, endMinute, endSecond, endMillisecond)
                            );
                        //显示的文本
                        strSub.AppendLine(jsonDatas[i].text);
                        strSub.AppendLine();
                        byte[] bufferWrite = Encoding.UTF8.GetBytes(strSub.ToString());
                        fsSrt.Write(bufferWrite, 0, bufferWrite.Length);
                        fsSrt.Flush();
                    }
                }
            }


        }

        public class JsonData 
        {
            public string text { get; set; }

            public double time_begin { get; set; }

            public double time_end { get; set;}
            public int text_begin { get; set; }
            public int text_end { get; set; }

        }

    }

    public class Config
    {
        /// <summary>
        /// 
        /// </summary>
        SaveConfig SaveConfig { get; set; } = new SaveConfig();

        /// <summary>
        /// 音色
        /// </summary>
        public string TimeBre { get; set; } = "male-qn-qingse";


    }

    /// <summary>
    /// 保存配置
    /// </summary>
    public class SaveConfig 
    {
        /// <summary>
        /// 音频文件保存路径
        /// </summary>
        public string? SaveAudioPath { get; set; }

        /// <summary>
        /// 字幕文件保存路径
        /// </summary>
        public string? SaveCaptionPath { get; set; }
    }
}
